package com.xxx.buffer;

/**
 * @version v1.0
 * @Description:
 * @Author zhanzhongxin
 * @Date 2021/7/19 14:05
 * @Copyright 博为峰
 */
public class StringBufferDemo {
    public static void main(String[] args) {
        /*
           StringBuffer: 字符串缓存区，数据结构是字符数组
           1.线程安全
           2.长度和内容可变
           3.可以存储任意类型的数据，但最终都会转成字符串输出

           StringBuilder
           1.线程不安全

           操作:
           1.增
           2.删
           3.改
           4.查

         */

//        add();
//        delete();
//        update();
//        query();
    }

    public static void query() {
        StringBuffer buffer = new StringBuffer();
        buffer.append("abc").append(100).append("def");//abc100def
        System.out.println(buffer.indexOf("a"));
        System.out.println(buffer.lastIndexOf("a"));
        System.out.println(buffer.indexOf("a", 1));
    }

    public static void update() {
        StringBuffer buffer = new StringBuffer();
        buffer.append("abc").append(100).append("def");//abc100def
        buffer.replace(2, 4, "A");//abA00def
        buffer.setCharAt(2, 'B');//abB00def
        System.out.println(buffer);
    }

    public static void delete() {
        StringBuffer buffer = new StringBuffer();
        buffer.append("abc").append(100).append("def");//abc100def
        buffer.delete(2, 4);//ab00def
        buffer.deleteCharAt(2);//ab0def
        System.out.println(buffer);
    }

    public static void add() {
        String s1 = "abc";
        String s2 = s1.concat("d");
//        System.out.println(s1 == s2);

        StringBuffer buffer = new StringBuffer();//初始数组长度是16
        StringBuffer buffer1 = buffer.append("abc");
//        System.out.println(buffer1 == buffer);
        buffer.append(100).append(true).insert(1, "A");//abc100true
        System.out.println(buffer);
    }
}
